package com.whu.helpluojia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.whu.helpluojia.common.UserException;
import com.whu.helpluojia.dao.AdminMessageDao;
import com.whu.helpluojia.dao.MessageDao;
import com.whu.helpluojia.domain.AdminMessage;
import com.whu.helpluojia.domain.Message;
import com.whu.helpluojia.domain.User;
import com.whu.helpluojia.domain.dto.MessageDto;
import com.whu.helpluojia.service.MessageService;
import com.whu.helpluojia.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class MessageServiceImpl extends ServiceImpl<MessageDao, Message> implements MessageService {

    @Autowired
    MessageDao messageDao;

    @Autowired
    UserService userService;

    @Autowired
    AdminMessageDao adminMessageDao;

    @Override
    public List<Message> getUserMessage(Long userId, Long id, int page) {
        List<Message> messages = new ArrayList<>();
        LambdaQueryWrapper<Message> qw = new LambdaQueryWrapper<Message>();
        qw.eq(Message::getSenderId, userId).eq(Message::getReceiverId, id).or();
        qw.eq(Message::getSenderId, id).eq(Message::getReceiverId, userId);
        IPage iPage = new Page(page, 10).addOrder(OrderItem.desc("id"));
        try {
            messages = messageDao.selectPage(iPage, qw).getRecords();
        }catch (Exception e){
            throw new UserException(e.getMessage());
        }
        return messages;
    }

    @Override
    public void addMessage(Message message) {

        messageDao.insert(message);

    }

    @Override
    public List<MessageDto> getHomeMessage(Long userId) {

        List<Message>messages=new ArrayList<>();

        LambdaQueryWrapper<Message> qw = new LambdaQueryWrapper<Message>();

        qw.eq(Message::getReceiverId,userId);
        qw.groupBy(Message::getSenderId);
        qw.orderBy(true,false,Message::getId);

        messages=messageDao.selectList(qw);

        List<MessageDto> messageDtos=new ArrayList<>();

        for(Message message:messages){
            MessageDto messageDto=new MessageDto();
            BeanUtils.copyProperties(message,messageDto);
            User user=userService.selectUser(message.getSenderId());
            messageDto.setSenderName(user.getName());
            messageDtos.add(messageDto);
        }

        return messageDtos;
    }

    @Override
    public List<MessageDto> getAdminMessage(Long userId) {
        LambdaQueryWrapper<Message> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Message::getSenderId,1L).eq(Message::getReceiverId,userId);
        List<Message>messages=messageDao.selectList(queryWrapper);

        List<Long> ids=new ArrayList<>();
        for(Message message:messages){
            ids.add(message.getId());
        }

        List<AdminMessage> adminMassages = new ArrayList<>();
        for(Long id : ids){
            LambdaQueryWrapper<AdminMessage> queryWrapper1=new LambdaQueryWrapper<>();
            queryWrapper1.eq(AdminMessage::getMessageId,id);
            adminMassages.add(adminMessageDao.selectOne(queryWrapper1));
        }
        AdminMessage[] adminMassagesArray=adminMassages.toArray(new AdminMessage[0]);


        int i=0;
        List<MessageDto>messageDtos=new ArrayList<>();
        for(Message message:messages){
            MessageDto messageDto=new MessageDto();
            BeanUtils.copyProperties(message,messageDto);
            messageDto.setAdminMassage(adminMassagesArray[i]);
            i++;
            messageDtos.add(messageDto);
        }

        return messageDtos;
    }
}
